package com.wewins.fota.controller;

import com.wewins.fota.bean.dto.ResponseRst;
import com.wewins.fota.service.PkgVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    private final PkgVersionService service;

    @Autowired
    public ProductController(PkgVersionService service) {
        this.service = service;
    }


    @GetMapping("/query")
    public ResponseRst<List<String>> queryAllProducts() {
        return ResponseRst.<List<String>>builder()
                .data(service.findAllProducts())
                .build().withOk();
    }

    @GetMapping("/query/versions/{product}")
    public ResponseRst<List<String>> queryVersionsByProduct(@PathVariable String product) {
        return ResponseRst.<List<String>>builder()
                .data(service.findVersionsByProduct(product))
                .build().withOk();
    }

    @GetMapping("/query/versions/filter/{product}/{subVersionStr}")
    public ResponseRst<List<String>> queryVersionsByCache(@PathVariable String product, @PathVariable String subVersionStr) {
        return ResponseRst.<List<String>>builder()
                .data(service.queryVersionsByCache(product, subVersionStr))
                .build().withOk();
    }

    @GetMapping("/query/filter/{subProductStr}")
    public ResponseRst<List<String>> queryProductByCache(@PathVariable String subProductStr) {
        return ResponseRst.<List<String>>builder()
                .data(service.queryProductsByCache(subProductStr))
                .build().withOk();
    }
}
